#ifndef _DANIEL_KERR_GLTITLE_H
#define _DANIEL_KERR_GLTITLE_H

#include "driver.h"
#include "textbox.h"
#include "gltext.h"
#include "glScrollingText.h"

GAMESTATE titleScreen()
{
	glClearColor(0.0,0.0,0.0,0.0);

	glDisable(GL_DEPTH_TEST);	
	glDisable(GL_LIGHTING);
	glDisable(GL_BLEND);

	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();
	gluOrtho2D(0,X_DIM,Y_DIM,0);

	textBox* ng = new textBox(300,300,150,"  New Game");
	textBox* in = new textBox(300,330,150,"  Instructions");
	textBox* gr = new textBox(300,360,150,"  Graphics Options");
	textBox* qu = new textBox(300,390,150,"  Quit Game");

	float r = 0.2,g = 0.2,b = 0.2;
	int mult = 5;

	glScrollingText scroller("title.txt",5.0,true);
	
	Star_C belt;
	belt.set2Dstars(2000);

	Timer fps;
	SDL_Event event;
	while(true)
	{
		fps.start();

		glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);

		for(int i = 0; i < belt.NOS(); ++i)
		{
			if(i % 2 == 0)		{glColor4f(r, g, b, 0.9);}
			else				{glColor4f(1-r, 1-g, 1-b, 0.9);}

			glTranslatef(belt.sx(i),belt.sy(i),belt.sz(i));
			glutSolidSphere(belt.ss(i),10,10);
			glTranslatef(-belt.sx(i),-belt.sy(i),-belt.sz(i));
		}

		glLineWidth(1.0);
		glColor4f(1,1,1,1.0);
		scroller.draw();

		glLineWidth(8);
		glColor4f(0.5-r,0.5-g,0.5-b,1.0);
		writeText(0,200,2.0,2.0,"Stellar");
		glLineWidth(3.2);
		glColor4f(1-r,1-g,1-b,1.0);
		writeText(102,147,0.5,0.5,"Stellar");
		glColor4f(r,g,b,1.0);
		writeText(100,150,0.5,0.5,"Stellar");
		glLineWidth(1.0);
		glColor4f(1,1,1,1.0);
		writeText(125,170,0.1,0.1,"by the Mogs of War");
		writeText(0,600,0.1,0.1,"2007 Texas Tech University Computer Science");

		r += (0.001 * mult);
		g += (0.001 * mult);
		b += (0.001 * mult);

		if((r > 1)||(g > 1)||(b > 1)) mult = mult * -1;
		if((r < 0.2)||(g < 0.2)||(b < 0.2)) mult = mult * -1;

		ng->draw(); in->draw(); gr->draw(); qu->draw();

		SDL_GL_SwapBuffers();
		
		if(ng->getData() == 1)			{return GAME_OPT;}
		else if(in->getData() == 1)		{return INST;}
		else if(gr->getData() == 1)		{return GRAPH_OPT;}
		else if(qu->getData() == 1)		{return QUIT;}

		while(SDL_PollEvent(&event))
		{
			if(event.type == SDL_KEYDOWN)
			{
				switch(event.key.keysym.sym)
				{
					case SDLK_RETURN:
					case SDLK_n: return GAME_OPT; break;
					case SDLK_i: return INST; break;
					case SDLK_g: return GRAPH_OPT; break;
					case SDLK_q: return QUIT; break;
				};
			}
			if(event.type == SDL_MOUSEBUTTONDOWN)
			{
				if(ng->clicked(event.button.x, event.button.y))
				{ng->onClick();}
				if(in->clicked(event.button.x, event.button.y))
				{in->onClick();}
				if(gr->clicked(event.button.x, event.button.y))
				{gr->onClick();}
				else if(qu->clicked(event.button.x, event.button.y))
				{qu->onClick();}
			}
			if(event.type == SDL_MOUSEMOTION)
			{
				ng->setHover(ng->hover(event.motion.x,event.motion.y));
				in->setHover(in->hover(event.motion.x,event.motion.y));
				gr->setHover(gr->hover(event.motion.x,event.motion.y));
				qu->setHover(qu->hover(event.motion.x,event.motion.y));
			}
		}

		// Cap Frame Rate
		while(fps.get_ticks()<1000/35){}
	}
}

#endif